[ActionScript 3] Array subclasses cannot be deserialized, Error #1034

Posted by aaaidan on Stack Overflow See other posts from Stack Overflow or by aaaidan
Published on 2010-03-28T23:21:18Z Indexed on 2010/03/28 23:23 UTC
Read the original article Hit count: 398

I've just found a strange error when deserializing from a ByteArray, where Vectors cannot contain types that extend Array: there is a TypeError when they are deserialized.

TypeError: Error #1034: Type Coercion failed: cannot convert []@4b8c42e1 to com.myapp.ArraySubclass.
    at flash.utils::ByteArray/readObject()
    at com.myapp::MyApplication()[/Users/aaaidan/MyApp/com/myapp/MyApplication.as:99]

Here's how:

public class Application extends Sprite {
    public function Application() {
        // register the custom class
        registerClassAlias("MyArraySubclass", MyArraySubclass);        

        // write a vector containing an array subclass to a byte array
        var vec:Vector.<MyArraySubclass> = new Vector.<MyArraySubclass>();
        var arraySubclass:MyArraySubclass = new MyArraySubclass();
        arraySubclass.customProperty = "foo";
        vec.push(arraySubclass);

        var ba:ByteArray = new ByteArray();
        ba.writeObject(arraySubclass);
        ba.position = 0;

        // read it back
        var arraySubclass2:MyArraySubclass = ba.readObject() as MyArraySubclass; // throws TypeError
    }
}

public class MyArraySubclass extends Array {
    public var customProperty:String = "default";
}

It's a pretty specific case, but it seems very odd to me. Anyone have any ideas what's causing it, or how it could be fixed?

© Stack Overflow or respective owner

Related posts about serialization

Related posts about actionscript-3